package com.own.business.exam.controller;


import com.own.business.exam.business.service.ExamRuleService;
import com.own.business.exam.entity.dto.ExamRuleDto;
import com.own.business.exam.entity.query.ExamRuleQuery;
import com.own.business.exam.entity.vo.ExamRuleVo;
import com.own.component.common.base.controller.BaseController;
import com.own.component.common.model.PageModel;
import com.own.component.common.model.R;
import com.own.component.common.model.ResultModel;
import com.own.component.store.core.confirm.aop.ConfirmAop;
import com.own.constant.ConstantApiPrefix;
import com.own.constant.ConstantBeanSuffix;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * ExamRuleController
 *
 * @author chenxueli
 * @date 2021-06-18 10:21:00
 */
@Slf4j
@Tag(name = "考试规则" + ConstantBeanSuffix.CONTROLLER)
@RestController
@RequestMapping(ConstantApiPrefix.BASE_API_V1_PREFIX + "/exam/rule")
public class ExamRuleController extends BaseController {

    @Resource
    private ExamRuleService examRuleService;

    @Operation(summary = "查询全部信息")
    @GetMapping("/all")
    public ResultModel<List<ExamRuleVo>> all(
            @ModelAttribute ExamRuleQuery query
    ) {
        var pageModel = examRuleService.all(query);
        return R.success(pageModel);
    }

    @Operation(summary = "分页查询信息")
    @GetMapping("/page")
    public ResultModel<PageModel<ExamRuleVo>> page(
            @ModelAttribute ExamRuleQuery query
    ) {
        var pageModel = examRuleService.page(query);
        return R.success(pageModel);
    }

    @Operation(summary = "添加考试规则")
    @PostMapping("/")
    public ResultModel<ExamRuleVo> add(
            @Validated @ModelAttribute ExamRuleDto dto
    ) {
        dto.check();
        var updateModel = examRuleService.add(dto.toBo());
        return R.success(updateModel);
    }

    @Operation(summary = "修改考试规则")
    @PutMapping("/{examRuleId}")
    public ResultModel<ExamRuleVo> update(
            @Parameter(description = "考试规则id", required = true) @PathVariable Long examRuleId,
            @Validated @ModelAttribute ExamRuleDto dto
    ) {
        dto.check();
        var updateModel = examRuleService.update(examRuleId, dto.toBo());
        return R.success(updateModel);
    }

    @ConfirmAop(message = "确认删除该考试规则？（注意：删除之后无法找回数据）")
    @Operation(summary = "删除考试规则")
    @DeleteMapping("/{examRuleId}")
    public ResultModel<Boolean> delete(
            @Parameter(description = "考试规则id", required = true) @PathVariable Long examRuleId
    ) {
        var updateModel = examRuleService.delete(examRuleId);
        return R.success(updateModel);
    }

    @Operation(summary = "根据id获取信息")
    @GetMapping("/{examRuleId}")
    public ResultModel<ExamRuleVo> getById(
            @Parameter(description = "考试规则id", required = true) @PathVariable Long examRuleId
    ) {
        var vo = examRuleService.getById(examRuleId);
        return R.success(vo);
    }

}
